package com.dontouchmypet;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;


public class MainActivity extends Activity{
	
	//VARIABILI DI ISTANZA
	private Button buttonLoginFacebook;
	//Metodo onCreate() solo inizializzazione di variabili
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_main);
	    //Se ho già effettuato il login a facebook apro direttamente il menu principale della app
	    Session.openActiveSession(MainActivity.this, true, new Session.StatusCallback() {
	    	// callback when session changes state
	    	@Override
	    	public void call(Session session, SessionState state, Exception exception) {
	    	if (session.isOpened())
	    	{
			   // make request to the /me API
			   Request.newMeRequest(session, new Request.GraphUserCallback() {
			   // callback after Graph API response with user object
			   @Override
			   public void onCompleted(GraphUser user, Response response) {
			      if (user != null)
				  {
			    	 //Aperto facebook lancio la seconda Activity per la visualizzazione dei dati
			    	  Intent intent=new Intent(MainActivity.this,FacebookLoginActivity.class);
			    	  intent.putExtra("nome", user.getName());
			    	  intent.putExtra("cognome", user.getLastName());
			    	  startActivity(intent);
			    	  finish();
			    	  
			    	  
				  }
			   }
			   }).executeAsync();
			   }}});
	    //inizializzo il bottone
	    buttonLoginFacebook = (Button) findViewById(R.id.buttonFacebookLogin);
	    buttonLoginFacebook.setOnClickListener(new View.OnClickListener() {	
	    	//Evento abilitato quando si richiede il login tramite facebook
			@Override
			public void onClick(View v) {
				Session.openActiveSession(MainActivity.this, true, new Session.StatusCallback() {
					// callback when session changes state
					@Override
					public void call(Session session, SessionState state, Exception exception) {
					if (session.isOpened())
					{
					   // make request to the /me API
					   Request.newMeRequest(session, new Request.GraphUserCallback() {
					   // callback after Graph API response with user object
					   @Override
					   public void onCompleted(GraphUser user, Response response) {
					      if (user != null)
						  {
					    	 //Aperto facebook lancio la seconda Activity per la visualizzazione dei dati 
					    	  Intent intent=new Intent(MainActivity.this,FacebookLoginActivity.class);
					    	  intent.putExtra("nome", user.getName());
					    	  intent.putExtra("cognome", user.getLastName());
					    	  startActivity(intent);
					    	  finish();
					    	  
						  }
					   }
					   }).executeAsync();
					   }}});
				
			}
		}); 
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
	}
	
	@Override
	protected void onPause()
	{
	   super.onPause();
	   //this.finish();
	}
	

	

}
